package code._17annotation._04handle;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

public class Demo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        //获取字节码文件对象
        Class<?> aClass = Class.forName("code._17annotation._04handle.Demo2");
        //获取Method对象
        Method loginMethod = aClass.getDeclaredMethod("login");
        //判断该方法是否使用了注解
        boolean annotationPresent = loginMethod.isAnnotationPresent(Login.class);
        if(annotationPresent){
            //使用了注解、获取注解实例中的信息
            Login annotation = loginMethod.getAnnotation(Login.class);
            String user = annotation.user();
            String password = annotation.password();
            System.out.println("user = " + user);
            System.out.println("password = " + password);
        }
    }

    @Login(user = "admin", password = "123456")
    public static void login() {

    }
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface Login {
    String user();

    String password();
}
